/*
 * RoomPanel.java
 *
 * Created on 16 mai 2010, 10:31:46
 */
package chipwits.room;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import javax.swing.Timer;

/**
 *
 * @author Emmanuel
 */
public class RoomPanel extends javax.swing.JPanel {

    public final static int TAILLE_CASE = 32;
    private MediaTracker mediaTracker;
    private SpriteManager spriteManager;
    private Image offScreenImage;
    private Graphics offScreenGraphicsCtx;
    private static Image[] listeItems = new Image[]{
        Toolkit.getDefaultToolkit().getImage("graphics/arg/mur.png"),
        Toolkit.getDefaultToolkit().getImage("graphics/arg/porte.png"),
        Toolkit.getDefaultToolkit().getImage("graphics/arg/sol.png"),
        Toolkit.getDefaultToolkit().getImage("graphics/arg/tux.png")
    };
    private int indexMediaTracker=0;

    public Image[] loadSprite(String path, int nbImages){
        Image[] images = new Image[nbImages];
        for(int i=0; i<nbImages; i++){
            images[i]=Toolkit.getDefaultToolkit().getImage(path+i+".png");
            mediaTracker.addImage(images[i], 1);
        }
        try {
            mediaTracker.waitForID(1);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
        return images;
    }

    /** Creates new form RoomPanel */
    public RoomPanel() {
        mediaTracker = new MediaTracker(this);  //en param, le compo sur lequel va etre eventually drawn
        for (int i = 0; i < listeItems.length; i++) {
            mediaTracker.addImage(listeItems[i], indexMediaTracker);
        }

        spriteManager = new SpriteManager();

        try {
            indexMediaTracker++;
            mediaTracker.waitForID(0);
        } catch (InterruptedException e) {
            System.out.println(e);
        }

        Image[] imBombe = loadSprite("graphics/arg/bombe/bombe0", 2);
        Sprite bombe1=new SpriteWarrior(new Point(128, 64), imBombe, 40, new Rectangle(200, 200), this, true);
        bombe1.setSpeedAnim(300);
        spriteManager.add(bombe1);

        Image[] imVirus = loadSprite("graphics/arg/virus/virus0", 8);
        Sprite virus1=new SpriteWarrior(new Point(64, 64), imVirus, 100, new Rectangle(200, 200), this, true);
        spriteManager.add(virus1);

        Sprite virus2=new SpriteWarrior(new Point(64, 32), imVirus, 50, new Rectangle(200, 200), this, true);
        virus2.move(0, 2);

        spriteManager.add(virus2);

        Sprite virus3=new SpriteWarrior(new Point(0, 0), imVirus, 30, new Rectangle(200, 200), this, true);
        virus3.move(0, 3);
        spriteManager.add(virus3);

        Image[] imPlayerU = loadSprite("graphics/player/haut0", 3);
        Image[] imPlayerD = loadSprite("graphics/player/bas0", 3);
        Image[] imPlayerL = loadSprite("graphics/player/gauche0", 3);
        Image[] imPlayerR = loadSprite("graphics/player/droite0", 3);
        Sprite player=new SpriteWarrior(new Point(320, 192), imPlayerL, 50, new Rectangle(200, 200), this, true);
        spriteManager.add(player);
        player.move(3, 3);

        //Mise à jour de l'affichage
        int delay = 40; //milliseconds
        ActionListener taskPerformer = new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                spriteManager.updatePosSprites();
                repaint();
            }
        };
        new Timer(delay, taskPerformer).start();
    }

    private static void loadRoom(String file) {
        FileReader fileRead = null;
        try {
            fileRead = new FileReader(file);
            StreamTokenizer in = new StreamTokenizer(fileRead);
            double tok;

            tok = in.nextToken();
            if (tok == StreamTokenizer.TT_NUMBER) {
                //PanelParamNun.offset[i] = (int) in.nval;
            }
        } catch (IOException ex) {
        }

    }

    private static void saveRoom(String file) {
        PrintWriter ecrivain = null;
        try {
            ecrivain = new PrintWriter(new FileWriter(file, false));
            ecrivain.print("prout");
            ecrivain.println(" !");
        } catch (IOException ex) {
        } finally {
            ecrivain.println("\n#Par Emmanuel Roussel");
            ecrivain.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="gestion de l'affichage">
    @Override
    public void paintComponent(Graphics g) {
        if (offScreenGraphicsCtx == null) {
            offScreenImage = createImage(getSize().width, getSize().height);
            offScreenGraphicsCtx = offScreenImage.getGraphics();
        }
        offScreenGraphicsCtx.setColor(new java.awt.Color(204, 255, 204));
        offScreenGraphicsCtx.fillRect(0, 0, getSize().width, getSize().height);

        offScreenGraphicsCtx.setColor(new Color(0.4f, 0.4f, 0f, 0.2f));
        for (int i = 0; i <= 10 * TAILLE_CASE; i += TAILLE_CASE) {
            offScreenGraphicsCtx.drawLine(0, i, 10 * TAILLE_CASE, i);
            offScreenGraphicsCtx.drawLine(i, 0, i, 10 * TAILLE_CASE);
        }
        // Draw the sprites offscreen
        spriteManager.redrawSprites(offScreenGraphicsCtx);

        // Draw the scene onto the screen
        if (offScreenImage != null) {
            g.drawImage(offScreenImage, 0, 0, this);
        }
    }

    /* @Override
    public void paint(Graphics g){
    }

    @Override
    public void update(Graphics g) {

    }*/
    // </editor-fold>
}
